package com.fwj.authorization.staff.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fwj.authorization.staff.entity.SysDict;
import com.fwj.authorization.staff.entity.SysDictItem;
import com.fwj.authorization.staff.entity.vo.SysDictVO;
import com.fwj.authorization.staff.service.SysDictItemService;
import com.fwj.authorization.staff.service.SysDictService;
import com.fwj.base.entity.R;
import com.fwj.base.exception.CustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 数据字典控制器
 *
 * @author 樊温军
 * @date 2025/3/18 上午10:32
 */
@Slf4j
@RestController
@RequestMapping("/dict")
@Api(tags = "数据字典控制器")
@RequiredArgsConstructor
public class SysDictController {

    private final SysDictService sysDictService;


    /**
     * 数据字典—分页查询
     *
     * @param page    分页对象
     * @param sysDict 查询条件
     * @return R<IPage < SysDict>>
     */
    @ApiOperation(value = "数据字典—分页查询")
    @GetMapping("/page")
    public R<IPage<SysDict>> getPage(Page<SysDict> page, SysDict sysDict) {
        try {
            log.info("数据字典—分页查询，分页对象：{}，参数：{}", JSONUtil.toJsonStr(page), JSONUtil.toJsonStr(sysDict));
            IPage<SysDict> iPage = sysDictService.getPage(page, sysDict);
            log.info("数据字典—分页查询，返回结果：{}", JSONUtil.toJsonStr(iPage));
            return R.ok(iPage, "查询成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 数据字典项—列表
     *
     * @param sysDictItem 查询条件
     * @return R<List < SysDictItem>>
     */
    @ApiOperation(value = "数据字典项—列表")
    @GetMapping("/item/list")
    public R<List<SysDictItem>> getItemList(SysDictItem sysDictItem) {
        try {
            log.info("数据字典项—列表，参数：{}", JSONUtil.toJsonStr(sysDictItem));
            if (StringUtils.isEmpty(sysDictItem.getDictCode())) {
                return R.failed("字典编码不能为空！");
            }
            List<SysDictItem> sysDictItems = sysDictService.getItemList(sysDictItem);
            log.info("数据字典项—列表，返回结果：{}", JSONUtil.toJsonStr(sysDictItems));
            return R.ok(sysDictItems, "查询成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }


    /**
     * 数据字典—保存
     *
     * @param sysDictVO sysDictVO
     * @return R<List < SysDictItem>>
     */
    @ApiOperation(value = "数据字典—保存")
    @PostMapping("/save")
    public R<Long> saveDict(@Valid @RequestBody SysDictVO sysDictVO) {
        try {
            log.info("数据字典—保存，参数：{}", JSONUtil.toJsonStr(sysDictVO));
            Long id = sysDictService.saveDict(sysDictVO);
            log.info("数据字典—保存，返回结果：{}", id);
            return R.ok(id, "保存成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 数据字典项—批量删除
     *
     * @param ids ID列表
     * @return R<Boolean>
     */
    @ApiOperation(value = "数据字典项—批量删除")
    @PostMapping("/item/del")
    public R<Boolean> itemDel(@RequestBody List<Long> ids) {
        try {
            log.info("数据字典项—批量删除，参数：{}", JSONUtil.toJsonStr(ids));
            if (CollectionUtils.isEmpty(ids)) {
                return R.failed("ID列表不能为空！");
            }
            Boolean flag = sysDictService.itemDel(ids);
            log.info("数据字典项—批量删除，返回结果：{}", flag);
            return R.ok(flag, "删除成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }
}

